package educar.models;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import educar.db.JPA;

public class User {
  private static JPA jpa = new JPA();
  private int code;
  private String username;
  private String password;
  private String role;
  
  /**
   * 
   * @param codigo "dni de usuario"
   * @param username "nombre de usuario"
   * @param password "dni de usuario"
   * @param role "usuario (alumno, administrador,docente)"
   */
  public User(int codigo,String username, String password, String role) {
    this.setUsername(username);
	this.setPassword(password);
	this.setRole(role);
	this.setCode(codigo);
  }
  
  /**
   * 
   * @param username "nombre de usuario"
   * @param password "dni de usuario"
   * @return true "usuario existe"
   * @return false "usuario no existe"
   */
  public static boolean authenticate(String username, String password) {
    try {
	  User u = getUserByUsername(username);
      if (password.equals(u.getPassword())) {
	    Session.setCurrentUser(u);
		return true;
	  }
	} 
    catch (userNotFound ex) {
	  return false;
	}
    return false;
  }
  
  /**
   * Guarda y confirma la carga 
   * @return true "si usuario es guardado correctamente"
   * @return false "no se ha cargado correctamente"
   */
  public boolean save() {
    try {
      User.getUserByID(username);
	  return false;
	} 
    catch (userNotFound e1) {
	  String[] columns = { "id", "username", "password", "role" };
      PreparedStatement stm = jpa.newRecord("educar_dev.users", columns);
      try {
	    stm.setInt(1,code);
		stm.setString(2, username);
		stm.setString(3, password);
		stm.setString(4, role);
	  } 
      catch (SQLException e) {
	    e.printStackTrace();
		return false;
      }
      jpa.create(stm);
      return true;
      }
  }
  
  /**
   * Elimina un usuario
   */
  public void destroy() {
	jpa.destroy("users", "id", Integer.toString(this.getCode())); 
  }
  
  /**
   * 
   * @param username "nombre de usuario"
   * @return u de tipo usuario
   * @throws userNotFound
   */
  public static User getUserByUsername(String username) throws userNotFound {
    ResultSet rs = null;
	User u = null;
	rs = jpa.getByField("users", "username", username);
	try {
	  if (rs.next()) {
	    u = new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
	  } 
	  else {
	    throw new userNotFound();
	  }
	} 
	catch (SQLException e) {
	  e.printStackTrace();
	}
    return u;
  }
  
  /**	
   * 
   * @param id "codigo de usuario"
   * @return u de tipo usuario
   * @throws userNotFound
   */
  public static User getUserByID(String id) throws userNotFound {
	ResultSet rs = null;
	User u = null;
	rs = jpa.getByField("users","id",id);
	try {
	  if (rs.next()) {
	    u = new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
	  } 
	  else {
	    throw new userNotFound();
	  }
    } 
	catch (SQLException e) {
	  e.printStackTrace();
	}
	return u;
  }

  /**
   * 
   * @return username "nombre se usuario"
   */
  public String getUsername() {
    return username;
  }
  
  /**
   * 
   * @param username "nombre de usuario"
   */
  public void setUsername(String username) {
	this.username = username;
  }
  
  /**
   * 
   * @return "dni de usuario"
   */
  public String getPassword() {
    return password;
  }
  
  /**
   * 
   * @return 
   */
  public JPA getConection() {
    return jpa;
  }
  
  /**
   * 
   * @param password "dni de usuario"
   */
  public void setPassword(String password) {
    this.password = password;
  }
  
  /**
   * 
   * @return role "rol de usuario (alumno, docente, administrador)"
   */
  public String getRole() {
	return role;
  }
  
  /**
   * 
   * @param role "rol de usuario (alumno, docente, administrador)"
   */
  public void setRole(String role) {
    this.role = role;
  }
  
  /**
   * 
   * @return code
   */
  public int getCode() {
    return code;
  }
  
  /**
   * 
   * @param i
   */
  public void setCode(int i){
	code=i;
  }
}
